clear all
set more off
set graphics off
capture log close

set maxvar 32000
 

global dates_coefplot_mobility_sf "_21946 _21947 _21948 _21949 _21950 _21951 _21952 _21953 _21954 _21955 _21956 _21957 _21958 _21959 _21960 _21961 _21962 _21963 _21964 _21965 _21966 _21967 _21968 _21969 _21970 _21971 _21972 _21973 _21974 _21975 _21976 _21977 _21978 _21979 _21980 _21981 _21982 _21983 _21984 _21985 _21986 _21987 _21988 _21989 _21990 _21991 _21992 _21993 _21994 _21995 _21996 _21997 _21998 _21999 _22000 _22001 _22002 _22003 _22004 _22005 _22006 _22007 _22008 _22009 _22010 _22011 _22012 _22013 _22014 _22015 _22016 _22017 _22018 _22019 _22020 _22021 _22022 _22023 _22024 _22025 _22026 _22027 _22028 _22029 _22030 _22031 _22032 _22033 _22034 _22035"


global other_rtgs_fxnc "msnbc_rtg cnn_rtg"
global other_rtgs_cnn "msnbc_rtg fxnc_rtg"
global other_rtgs_msnbc "fxnc_rtg cnn_rtg"

global other_ch_fxnc "msnbc cnn"
global other_ch_cnn "msnbc fxnc"
global other_ch_msnbc "fxnc cnn"


global outcomes_pick4 "tot_expgroc"
global outcomes_pick2 "sd_sf_dist_fr_home sd_sf_median_nohome_time sd_sf_pt_work_dev sd_sf_ft_work_dev sd_sf_home_dev_count"
global outcomes_pick1 "ratio_cases_pop ratio_deaths_pop st_incr_cases_7_ st_incr_deaths_7_"

* invert channel positions for representation purposes
replace fxnc=-fxnc
replace cnn=-cnn
replace msnbc=-msnbc


global full_controls1 "pop_dens_county landarea_county age_imb pct_food_stamps_county med_rep16 med_rep12 pct_nohs_county pct_college_county med_white med_black pct_male_county"


* SUM STATS FOR WHOLE SAMPLE, STATS ARE ON REGRESSIONS SAMPLE
global sum_stats=1
if ${sum_stats}==1 {
sutex rtgxxfxnc_2020 rtgxxcnn_2020 rtgxxmsnbc_2020 orig_pos_fxnc_w orig_pos_cnn_w orig_pos_msnbc_w pop pop_dens_county landarea_county age_imb pct_food_stamps_county med_rep16 med_rep12 pct_nohs_county pct_college_county med_white med_black pct_male_county fips_county state_fips, minmax
}

* FIRST STAGE
global first_stage=1
if ${first_stage}==1 {

* First stage DISTANCE TIME CONSTANT SPECIFICATION
preserve
foreach network in fxnc {
replace `network'=-`network'
eststo: ivreghdfe sf_dist_fr_home22038 (rtgxxfxnc_2020=`network') [aweight=pop], absorb(state_fips one##c.($full_controls1 ${other_rtgs_`network'})) cluster(state_fips) first
binscatter 	rtgxxfxnc_2020 `network' [aweight=pop], absorb(state_fips) controls($full_controls1 ${other_rtgs_`network'}) nquantiles(25) ${general_options_fs_`network'} text(1.28 1.2 "Slope = -.04***"  "(s.e. = .01)" "F-stat = 12.48", size(3) justification(left)) xsize(6)
graph export "analysis/data_alldates/channels_2016/paper_output_full/firststage_`network'_censuscontrols1_state_fips.pdf", replace
}
restore

preserve
foreach network in cnn msnbc {
replace `network'=-`network'
ivreghdfe sf_dist_fr_home22038 (`network'_rtg=`network') [aweight=pop], absorb(state_fips one##c.($full_controls1 ${other_rtgs_`network'})) cluster(state_fips) first
binscatter 	`network'_rtg `network' [aweight=pop], absorb(state_fips) controls($full_controls1 ${other_rtgs_`network'}) nquantiles(25) ${general_options_fs_`network'}
graph export "analysis/data_alldates/channels_2016/paper_output_full/firststage_`network'_censuscontrols1_state_fips.pdf", replace
}
restore

* First stage DISTANCE TIME VARYING SPECIFICATION (DAY OF THE WEEK BASELINE)
	forvalues date=22032/22038 {
	local y sf_dist_fr_home
	if dow`date'[1]==0{
	global baseline "`y'_sun"
	}					
	if dow`date'[1]==1{
	global baseline "`y'_mon"
	}					
	if dow`date'[1]==2{
	global baseline "`y'_tue"
	}					
	if dow`date'[1]==3{
	global baseline "`y'_wed"
	}					
	if dow`date'[1]==4{
	global baseline "`y'_thu"
	}					
	if dow`date'[1]==5{
	global baseline "`y'_fri"
	}					
	if dow`date'[1]==6{
	global baseline "`y'_sat"
	}
	preserve
		foreach network in fxnc {
		replace `network'=-`network'
		ivreghdfe `y'`date' (`network'_rtg=`network') [aweight=pop], absorb(state_fips one##c.($baseline $full_controls1 ${other_rtgs_`network'})) cluster(state_fips) first
		binscatter 	`network'_rtg `network' [aweight=pop], absorb(state_fips) controls($baseline ${other_rtgs_`network'}) nquantiles(25) ${general_options_fs_`network'}
		macro drop baseline
		graph export "analysis/data_alldates/channels_2016/paper_output_full/firststage_`network'_`date'_censuscontrols1_state_fips.pdf", replace
		}
	restore
	}


* First stage PURCHASES
preserve
foreach network in fxnc  {
replace `network'=-`network'
ivreghdfe sd_tot_expgroc22038 (`network'_rtg=`network') [aweight=pop], absorb(state_fips one##c.($full_controls1 ${other_rtgs_`network'})) cluster(state_fips) first
binscatter 	`network'_rtg `network' [aweight=pop], absorb(state_fips) controls($full_controls1 ${other_rtgs_`network'}) nquantiles(19) ${general_options_fs_`network'}
graph export "analysis/data_alldates/channels_2016/paper_output_full/firststage_purcheses_`network'_censuscontrols1_state_fips.pdf", replace
}
restore

* First stage CHANNELS CONTROLS
preserve
foreach network in fxnc cnn msnbc  {
replace `network'=-`network'
ivreghdfe sf_dist_fr_home22038 (`network'_rtg=`network') [aweight=pop], absorb(state_fips one##c.(${other_ch_`network'} $full_controls1 ${other_rtgs_`network'})) cluster(state_fips) first
binscatter 	`network'_rtg `network' [aweight=pop], absorb(state_fips) controls(${other_ch_`network'} $full_controls1 ${other_rtgs_`network'}) nquantiles(19) ${general_options_fs_`network'}
graph export "analysis/data_alldates/channels_2016/paper_output_full/firststage_network_check_`network'_censuscontrols1_state_fips.pdf", replace
}
restore

}
*

* REGRESSION TABLES
global regressions=1
if ${regressions}==1{
	foreach y in sf_dist_fr_home sf_median_nohome_time {
	eststo clear
		forvalues date=21946/22035 {
			if dow`date'[1]==0{
			global baseline "`y'_sun"
			}					
			if dow`date'[1]==1{
			global baseline "`y'_mon"
			}					
			if dow`date'[1]==2{
			global baseline "`y'_tue"
			}					
			if dow`date'[1]==3{
			global baseline "`y'_wed"
			}					
			if dow`date'[1]==4{
			global baseline "`y'_thu"
			}					
			if dow`date'[1]==5{
			global baseline "`y'_fri"
			}					
			if dow`date'[1]==6{
			global baseline "`y'_sat"
			}
		cap qui eststo: ivreghdfe `y'`date' (fxnc_rtg=fxnc) [aweight=pop], absorb(state_fips one##c.($baseline $full_controls1 ${other_rtgs_fxnc})) cluster(state_fips)
		}
	esttab using "analysis/data_alldates/channels_2016/paper_output_full/iv/nosd_2016_fxnc_censuscontrols1_state_fips_`y'.csv", unstack ar2 se replace  star(+ 0.10 * 0.05 ** 0.01 @ 0.001)
	}

	foreach y in st_incr_cases_7_ st_incr_deaths_7_ {
	eststo clear
		forvalues date=21946/22035 {
			if dow`date'[1]==0{
			global baseline "`y'_sun"
			}					
			if dow`date'[1]==1{
			global baseline "`y'_mon"
			}					
			if dow`date'[1]==2{
			global baseline "`y'_tue"
			}					
			if dow`date'[1]==3{
			global baseline "`y'_wed"
			}					
			if dow`date'[1]==4{
			global baseline "`y'_thu"
			}					
			if dow`date'[1]==5{
			global baseline "`y'_fri"
			}					
			if dow`date'[1]==6{
			global baseline "`y'_sat"
			}
		cap qui eststo: ivreghdfe `y'`date' (fxnc_rtg=fxnc) [aweight=pop], absorb(state_fips one##c.($full_controls1 ${other_rtgs_fxnc})) cluster(state_fips)
		}
	esttab using "analysis/data_alldates/channels_2016/paper_output_full/iv/2016_fxnc_censuscontrols1_state_fips_`y'.csv", unstack ar2 se replace  star(+ 0.10 * 0.05 ** 0.01 @ 0.001)
	}
	
preserve
foreach var in pop_dens_county age_imb pct_food_stamps_county med_rep16 med_rep12 pct_occ_serv_county	landarea_county pct_nohs_county  pct_college_county med_white med_black pct_male_county {
egen sd_`var'=sd(`var')
replace `var'=`var'/sd_`var'
}

foreach var in fxnc {
* Checks on Instrument
cap qui eststo: reghdfe age_imb `var' pop_dens_county   pct_food_stamps_county med_rep16 med_rep12 landarea_county pct_nohs_county  	pct_college_county med_white med_black pct_male_county [aweight=pop] , absorb(state_fips one##c.(cnn_rtg msnbc_rtg )) cluster(state_fips) nocons 

cap qui eststo: reghdfe pct_food_stamps_county `var'  pop_dens_county age_imb  med_rep16 med_rep12 pct_nohs_county  landarea_county	pct_college_county med_white med_black pct_male_county [aweight=pop] , absorb(state_fips one##c.(cnn_rtg msnbc_rtg )) cluster(state_fips) nocons 

cap qui eststo: reghdfe pct_college_county `var'  pop_dens_county age_imb pct_food_stamps_county med_rep16 med_rep12 landarea_county pct_nohs_county  med_white med_black pct_male_county [aweight=pop] , absorb(state_fips one##c.(cnn_rtg msnbc_rtg )) cluster(state_fips) nocons 

cap qui eststo: reghdfe pct_male_county `var'  pop_dens_county age_imb pct_food_stamps_county med_rep16 med_rep12	landarea_county pct_nohs_county pct_college_county med_white med_black  [aweight=pop] , absorb(state_fips one##c.(cnn_rtg msnbc_rtg )) cluster(state_fips) nocons 

cap qui eststo: reghdfe pct_nohs_county `var'  pop_dens_county age_imb pct_food_stamps_county med_rep16 med_rep12	landarea_county  pct_college_county med_white med_black pct_male_county [aweight=pop] , absorb(state_fips one##c.(cnn_rtg msnbc_rtg )) cluster(state_fips) nocons 

cap qui eststo: reghdfe med_black `var'  pop_dens_county age_imb pct_food_stamps_county med_rep16 med_rep12	landarea_county pct_nohs_county pct_college_county med_white pct_male_county [aweight=pop] , absorb(state_fips one##c.(cnn_rtg msnbc_rtg )) cluster(state_fips) nocons 

cap qui eststo: reghdfe med_white `var'  pop_dens_county age_imb pct_food_stamps_county med_rep16 med_rep12	landarea_county pct_nohs_county pct_college_county med_black pct_male_county [aweight=pop] , absorb(state_fips one##c.(cnn_rtg msnbc_rtg )) cluster(state_fips) nocons 

cap qui eststo: reghdfe med_rep16 `var'  pop_dens_county age_imb pct_food_stamps_county  landarea_county pct_nohs_county pct_college_county med_white med_black   pct_male_county [aweight=pop] , absorb(state_fips one##c.(cnn_rtg msnbc_rtg )) cluster(state_fips) nocons 

cap qui eststo: reghdfe med_rep12 `var'  pop_dens_county age_imb pct_food_stamps_county med_rep16  landarea_county pct_nohs_county pct_college_county med_white med_black   pct_male_county [aweight=pop] , absorb(state_fips one##c.(cnn_rtg msnbc_rtg )) cluster(state_fips) nocons

cap qui eststo: reghdfe pop_dens_county `var'   age_imb pct_food_stamps_county med_rep16 med_rep12 landarea_county 	 pct_nohs_county  pct_college_county med_white med_black pct_male_county [aweight=pop] , absorb(state_fips one##c.(cnn_rtg msnbc_rtg )) cluster(state_fips) nocons 

cap qui eststo: reghdfe landarea_county  `var'   pop_dens_county age_imb pct_food_stamps_county med_rep16	med_rep12 pct_nohs_county pct_college_county med_white med_black pct_male_county [aweight=pop] , absorb(state_fips one##c.(cnn_rtg msnbc_rtg )) cluster(state_fips) nocons

esttab using "analysis/data_alldates/channels_2016/paper_output_full/instrument_balancecheck_county.csv", ar2 se replace  star(* 0.05 ** 0.01)

}
restore


}
*

* DECADATA (PROTECTIVE GOODS) - NO BASELINE
global decadata=1
if ${decadata}==1 {
* decadata weekly *
foreach pre in sd_ {
	foreach cl in state_fips {
		foreach network in fxnc {

		* Main outcomes REDUCED
			foreach y of global outcomes_pick4  {
			cap drop _est*
			eststo clear
				forvalues date=21946/22035 {
				cap qui reghdfe `pre'`y'_w_`date' `network' [aweight=pop], absorb(state_fips one##c.($full_controls1) ${other_rtgs_`network'}) cluster(`cl')
				}
			}
		*	
		* Main outcomes OLS
			foreach y of global outcomes_pick4  {
			cap drop _est*
			eststo clear
				forvalues date=21946/22035 {
				cap qui reghdfe `pre'`y'_w_`date' `network'_rtg [aweight=pop], absorb(state_fips one##c.($full_controls1) ${other_rtgs_`network'}) cluster(`cl')
				}
			}
		*		
		* Main outcomes IV
			foreach y of global outcomes_pick4  {
			cap drop _est*
			eststo clear
				forvalues date=21946/22035 {
				cap qui ivreghdfe `pre'`y'_w_`date' (`network'_rtg=`network') [aweight=pop], absorb(state_fips one##c.($full_controls1) ${other_rtgs_`network'}) cluster(`cl')
				cap eststo _`date'
				}
			}
		*			
		
		}
	}
}
*

*	
}
*

* DECADATA (PROTECTIVE GOODS) - WITH BASELINE *
foreach cl in state_fips {
	foreach network in fxnc {

	* Main outcomes REDUCED
			foreach y of global outcomes_pick4 {
				cap drop _est*				
					forvalues date=21946/22035 {
					gen date_temp=`date'
					gen month_temp=month(date_temp)
					gen day_temp=day(date_temp)
					local month_temp=month_temp[1]
					local day_temp=day_temp[1]
					drop date_temp month_temp day_temp
					cap qui reghdfe `y'`date' `network' [aweight=pop], absorb(state_fips one##c.(bl_`y'_`month_temp'_`day_temp' $full_controls1 ${other_rtgs_`network'})) cluster(`cl')
					}
				}	
	*
	* Main outcomes OLS
			foreach y of global outcomes_pick4 {
				cap drop _est*				
					forvalues date=21946/22035 {
					gen date_temp=`date'
					gen month_temp=month(date_temp)
					gen day_temp=day(date_temp)
					local month_temp=month_temp[1]
					local day_temp=day_temp[1]
					drop date_temp month_temp day_temp
					cap qui reghdfe `y'`date' `network'_rtg [aweight=pop], absorb(state_fips one##c.(bl_`y'_`month_temp'_`day_temp' $full_controls1 ${other_rtgs_`network'})) cluster(`cl')
					}
				}
	*
	* Main outcomes IV
			foreach y of global outcomes_pick4 {
				cap drop _est*	
					forvalues date=21946/22035 {
					gen date_temp=`date'
					gen month_temp=month(date_temp)
					gen day_temp=day(date_temp)
					local month_temp=month_temp[1]
					local day_temp=day_temp[1]
					drop date_temp month_temp day_temp
					cap qui ivreghdfe `y'`date' (`network'_rtg= `network') [aweight=pop], absorb(state_fips one##c.(bl_`y'_`month_temp'_`day_temp' $full_controls1 ${other_rtgs_`network'})) cluster(`cl')
					}
				}
	*
	}
}
*	

* SAFEGRAPH - WITH BASELINE (DAY OF THE WEEK, AVG OF JAN 2020)
global safegraph=1
if ${safegraph}==1 {
* safegraph with BASELINE*
foreach cl in state_fips {
	foreach network in fxnc cnn msnbc {

	* Main outcomes REDUCED
			foreach y of global outcomes_pick2 {
				cap drop _est*
				eststo clear
					forvalues date=21946/22035 {
					if dow`date'[1]==0{
					global baseline "`y'_sun"
					}					
					if dow`date'[1]==1{
					global baseline "`y'_mon"
					}					
					if dow`date'[1]==2{
					global baseline "`y'_tue"
					}					
					if dow`date'[1]==3{
					global baseline "`y'_wed"
					}					
					if dow`date'[1]==4{
					global baseline "`y'_thu"
					}					
					if dow`date'[1]==5{
					global baseline "`y'_fri"
					}					
					if dow`date'[1]==6{
					global baseline "`y'_sat"
					}

					cap qui reghdfe `y'`date' `network' [aweight=pop], absorb(state_fips one##c.($baseline $full_controls1 ${other_rtgs_`network'})) cluster(`cl')
					macro drop time_case time_death baseline
					}
				}	
	*
	* Main outcomes OLS
			foreach y of global outcomes_pick2 {
				cap drop _est*
				eststo clear
					forvalues date=21946/22035 {	
					if dow`date'[1]==0{
					global baseline "`y'_sun"
					}					
					if dow`date'[1]==1{
					global baseline "`y'_mon"
					}					
					if dow`date'[1]==2{
					global baseline "`y'_tue"
					}					
					if dow`date'[1]==3{
					global baseline "`y'_wed"
					}					
					if dow`date'[1]==4{
					global baseline "`y'_thu"
					}					
					if dow`date'[1]==5{
					global baseline "`y'_fri"
					}					
					if dow`date'[1]==6{
					global baseline "`y'_sat"
					}

					cap qui reghdfe `y'`date' `network'_rtg [aweight=pop], absorb(state_fips one##c.($baseline $full_controls1 ${other_rtgs_`network'})) cluster(`cl')
					macro drop time_case time_death baseline
					}
				}
	*
	* Main outcomes IV
			foreach y in sd_sf_dist_fr_home sd_sf_median_nohome_time {
				cap drop _est*
				eststo clear
					forvalues date=21946/22035 {
					if dow`date'[1]==0{
					global baseline "`y'_sun"
					}					
					if dow`date'[1]==1{
					global baseline "`y'_mon"
					}					
					if dow`date'[1]==2{
					global baseline "`y'_tue"
					}					
					if dow`date'[1]==3{
					global baseline "`y'_wed"
					}					
					if dow`date'[1]==4{
					global baseline "`y'_thu"
					}					
					if dow`date'[1]==5{
					global baseline "`y'_fri"
					}					
					if dow`date'[1]==6{
					global baseline "`y'_sat"
					}

					cap qui ivreghdfe `y'`date' (`network'_rtg= `network') [aweight=pop], absorb(state_fips one##c.($baseline $full_controls1 ${other_rtgs_`network'})) cluster(`cl')
					cap eststo _`date'
					macro drop time_case time_death baseline
					}
				}
	*
	}
}
*				
}
*

* SAFEGRAPH - NO BASELINE
global rc_safegraph=1
if ${rc_safegraph}==1 {			
* safegraph no baseline *
foreach cl in state_fips {
	foreach network in fxnc {

	* Main outcomes REDUCED
			foreach y of global outcomes_pick2 {

				cap drop _est*
				eststo clear
					forvalues date=21946/22035 {
					cap qui reghdfe `y'`date' `network' [aweight=pop], absorb(state_fips one##c.( $full_controls1 ${other_rtgs_`network'})) cluster(`cl')
					}
				}	
	*
	* Main outcomes OLS
			foreach y of global outcomes_pick2 {
				cap drop _est*
				eststo clear
					forvalues date=21946/22035 {
					cap qui reghdfe `y'`date' `network'_rtg [aweight=pop], absorb(state_fips one##c.($full_controls1 ${other_rtgs_`network'})) cluster(`cl')
					}
				}
	*
	* Main outcomes IV
			foreach y of global outcomes_pick2 {
				cap drop _est*
				eststo clear
					forvalues date=21946/22035 {
					cap qui ivreghdfe `y'`date' (`network'_rtg= `network') [aweight=pop], absorb(state_fips one##c.($full_controls1 ${other_rtgs_`network'})) cluster(`cl')
					}
				}
	*
	}
}
*	

}		
*

* CASES AND DEATHS *
global corona=1
if ${corona}==1 {
foreach cl in state_fips {
	* Main outcomes REDUCED
		foreach network in fxnc {
				cap drop _est*
				eststo clear
					forvalues date=21946/22035 {
					cap qui reghdfe `y'`date' `network' [aweight=pop], absorb(state_fips one##c.($full_controls1 ${other_rtgs_`network'})) cluster(`cl')
					}
			}
	*
	* Main outcomes OLS
			foreach y of global outcomes_pick1 {
				cap drop _est*
				eststo clear
					forvalues date=21946/22035 {
					cap qui reghdfe `y'`date' `network'_rtg [aweight=pop], absorb(state_fips one##c.($full_controls1  ${other_rtgs_`network'})) cluster(`cl')
					}
				}
	* Main outcomes iv
			foreach y of global outcomes_pick1 {
				cap drop _est*
				eststo clear
					forvalues date=21946/22035 {	
					cap qui ivreghdfe `y'`date' (`network'_rtg=`network') [aweight=pop], absorb(state_fips one##c.($full_controls1  ${other_rtgs_`network'})) cluster(`cl')
					}
				}
	*		
		}
	}
*

* RESULTS WITH MAY and JUNE *
global corona_may=1
if ${corona_may}==1 {	
*
	foreach cl in state_fips {
		foreach network in fxnc {
		* Main outcomes iv
				foreach y of global outcomes_pick1 {
					cap drop _est*
					eststo clear
						forvalues date=21946/22096 {	
						cap qui ivreghdfe `y'`date' (`network'_rtg=`network') [aweight=pop], absorb(state_fips one##c.($full_controls1  ${other_rtgs_`network'})) cluster(`cl')
						}
					}
		*		
		}
	}
*

}
*

* SEASONAL FLU *
global other_outcomes "deaths_seas_flu2007_2009 deaths_seas_flu2010_2013 deaths_seas_flu2013_2015 deaths_seas_flu2016_2018"
foreach cl in state_fips {
	* Main outcomes REDUCED
		foreach y of global other_outcomes {
		if "`y'"=="deaths_seas_flu2007_2009"{
		global text "text(1850 -1.1 "Slope = 119.66"  "(s.e. = 153.02)", size(3) justification(left))"
		}
		if "`y'"=="deaths_seas_flu2010_2013"{
		global text "text(1850 -1.1 "Slope = 119.79"  "(s.e. = 141.36)", size(3) justification(left))"
		}
		if "`y'"=="deaths_seas_flu2013_2015"{
		global text "text(1850 -1.1 "Slope = 130.40"  "(s.e. = 151.21)", size(3) justification(left))"
		}
		if "`y'"=="deaths_seas_flu2016_2018"{
		global text "text(1850 -1.1 "Slope =  122.48"  "(s.e. = 147.67)", size(3) justification(left))"
		}
		
		reghdfe `y' fxnc [aweight=pop], absorb(state_fips one##c.($full_controls1 msnbc_rtg cnn_rtg)) cluster(`cl')
		binscatter 	`y' fxnc [aweight=pop], ${text} absorb(state_fips)  controls($full_controls1 ${other_rtgs_fxnc}) nquantiles(19) graphregion(margin(3 6 3 3)) play(y_title_coefplot_no_legenda) graphregion(fcolor(white)) legend(off) ytitle("Effect on Seasonal Flu (RF)")  xsize(8)
		cap macro drop text
		graph export "analysis/data_alldates/channels_2016/paper_output_full/reduced_form/2016_fxnc_censuscontrols1_`cl'_`y'.pdf", replace
		}

	*
	* Main outcomes OLS
		foreach y of global other_outcomes {
		reghdfe `y' fxnc_rtg [aweight=pop], absorb(state_fips one##c.($full_controls1 msnbc_rtg cnn_rtg)) cluster(`cl')
		binscatter 	`y' fxnc_rtg [aweight=pop], absorb(state_fips)  controls($full_controls1 ${other_rtgs_fxnc}) nquantiles(19)
		graph export "analysis/data_alldates/channels_2016/paper_output_full/ols/2016_fxnc_censuscontrols1_`cl'_`y'.pdf", replace
		}
	*
	* Main outcomes IV
		foreach y of global other_outcomes {
		ivreghdfe `y' (fxnc_rtg= fxnc) [aweight=pop], absorb(state_fips one##c.($full_controls1 msnbc_rtg cnn_rtg)) cluster(`cl') first
		binscatter 	`y' fxnc_rtg [aweight=pop], absorb(state_fips)  controls($full_controls1 ${other_rtgs_fxnc}) nquantiles(19)
		graph export "analysis/data_alldates/channels_2016/paper_output_full/iv/2016_fxnc_censuscontrols1_`cl'_`y'.pdf", replace
		}
		*
		}
*

* INSTRUMENT BALANCE CHECK *
global instrument_bcheck=1
if ${instrument_bcheck}==1 {
preserve
foreach var in pop_dens_county age_imb pct_food_stamps_county med_rep16 med_rep12 pct_occ_serv_county	landarea_county pct_nohs_county  pct_college_county med_white med_black pct_male_county {
egen sd_`var'=sd(`var')
replace `var'=`var'/sd_`var'
}
gen pop_dens_county_graph=pop_dens_county/10 /* FOR REPRESENTATION PURPOSES, OTHERWISE OTHER COEFS ARE UNREADEABLE */

foreach var in fxnc {
* Checks on Instrument
reghdfe age_imb `var' pop_dens_county   pct_food_stamps_county med_rep16 med_rep12 landarea_county pct_nohs_county  	pct_college_county med_white med_black pct_male_county [aweight=pop] , absorb(state_fips one##c.(cnn_rtg msnbc_rtg )) cluster(state_fips) nocons 
cap eststo coef_age_imb

reghdfe pct_food_stamps_county `var'  pop_dens_county age_imb  med_rep16 med_rep12 pct_nohs_county  landarea_county	pct_college_county med_white med_black pct_male_county [aweight=pop] , absorb(state_fips one##c.(cnn_rtg msnbc_rtg )) cluster(state_fips) nocons 
cap eststo coef_pct_food_stamps_county

reghdfe pct_college_county `var'  pop_dens_county age_imb pct_food_stamps_county med_rep16 med_rep12 landarea_county pct_nohs_county  med_white med_black pct_male_county [aweight=pop] , absorb(state_fips one##c.(cnn_rtg msnbc_rtg )) cluster(state_fips) nocons 
cap eststo coef_pct_college_county

reghdfe pct_male_county `var'  pop_dens_county age_imb pct_food_stamps_county med_rep16 med_rep12	landarea_county pct_nohs_county pct_college_county med_white med_black  [aweight=pop] , absorb(state_fips one##c.(cnn_rtg msnbc_rtg )) cluster(state_fips) nocons 
cap eststo coef_pct_male_county

reghdfe pct_nohs_county `var'  pop_dens_county age_imb pct_food_stamps_county med_rep16 med_rep12	landarea_county  pct_college_county med_white med_black pct_male_county [aweight=pop] , absorb(state_fips one##c.(cnn_rtg msnbc_rtg )) cluster(state_fips) nocons 
cap eststo coef_pct_nohs_county

reghdfe med_black `var'  pop_dens_county age_imb pct_food_stamps_county med_rep16 med_rep12	landarea_county pct_nohs_county pct_college_county med_white pct_male_county [aweight=pop] , absorb(state_fips one##c.(cnn_rtg msnbc_rtg )) cluster(state_fips) nocons 
cap eststo coef_black

reghdfe med_white `var'  pop_dens_county age_imb pct_food_stamps_county med_rep16 med_rep12	landarea_county pct_nohs_county pct_college_county med_black pct_male_county [aweight=pop] , absorb(state_fips one##c.(cnn_rtg msnbc_rtg )) cluster(state_fips) nocons 
cap eststo coef_white

reghdfe med_rep16 `var'  pop_dens_county age_imb pct_food_stamps_county  landarea_county pct_nohs_county pct_college_county med_white med_black   pct_male_county [aweight=pop] , absorb(state_fips one##c.(cnn_rtg msnbc_rtg )) cluster(state_fips) nocons 
cap eststo coef_med_rep16

reghdfe med_rep12 `var'  pop_dens_county age_imb pct_food_stamps_county med_rep16  landarea_county pct_nohs_county pct_college_county med_white med_black   pct_male_county [aweight=pop] , absorb(state_fips one##c.(cnn_rtg msnbc_rtg )) cluster(state_fips) nocons
cap eststo coef_med_rep12

reghdfe pop_dens_county_graph `var'   age_imb pct_food_stamps_county med_rep16 med_rep12 landarea_county 	 pct_nohs_county  pct_college_county med_white med_black pct_male_county [aweight=pop] , absorb(state_fips one##c.(cnn_rtg msnbc_rtg )) cluster(state_fips) nocons 
cap eststo coef_pop_dens_county_graph

reghdfe landarea_county  `var'   pop_dens_county age_imb pct_food_stamps_county med_rep16	med_rep12 pct_nohs_county pct_college_county med_white med_black pct_male_county [aweight=pop] , absorb(state_fips one##c.(cnn_rtg msnbc_rtg )) cluster(state_fips) nocons
cap eststo coef_landarea_county

coefplot (coef_pop_dens_county_graph, aseq(Pop Density)) (coef_age_imb, aseq(Age Imbalance)) (coef_pct_food_stamps_county, aseq(Food Stamps pct)) (coef_pct_college_county, aseq(College pct)) (coef_pct_male_county, aseq(Male pct)) (coef_pct_nohs_county, aseq(No High School pct)) (coef_black, aseq(Black pop ab. median)) (coef_white, aseq(White pop ab. median)) (coef_med_rep16, aseq(Median Republican 2016)) (coef_med_rep12, aseq(Median Republican 2012)) (coef_landarea_county, aseq(Land Area)),  play(y_title_coefplot_no_legenda) graphregion(fcolor(white)) legend(off) ytitle("Covariate Balance Check on Channel Position Instrument") xtitle("Coefficient on Z with 95% CI’s") xline(0, lpattern(dash) lcolor(gray))  ciopts(color(teal))  symbol(circle_hollow) msize(med) color(maroon) keep(fxnc)  aseq swapnames byopts(xrescale)
graph export "analysis/data_alldates/channels_2016/paper_output_full/instrument_checks_`var'_censuscontrols3_state_fips.pdf", replace
}
restore
}
*

* DROP ONE CONTROL REGS *
global drop_one_control=1
if ${drop_one_control}==1 {
	
global full_controls_var1 " 				landarea_county age_imb pct_food_stamps_county med_rep16 med_rep12 pct_nohs_county pct_college_county med_white med_black pct_male_county"
global full_controls_var2 " pop_dens_county 				age_imb pct_food_stamps_county med_rep16 med_rep12 pct_nohs_county pct_college_county med_white med_black pct_male_county"
global full_controls_var3 " pop_dens_county landarea_county 		pct_food_stamps_county med_rep16 med_rep12 pct_nohs_county pct_college_county med_white med_black pct_male_county"
global full_controls_var4 " pop_dens_county landarea_county age_imb					       med_rep16 med_rep12 pct_nohs_county pct_college_county med_white med_black pct_male_county"
global full_controls_var5 " pop_dens_county landarea_county age_imb pct_food_stamps_county 			 med_rep12 pct_nohs_county pct_college_county med_white med_black pct_male_county"
global full_controls_var6 " pop_dens_county landarea_county age_imb pct_food_stamps_county med_rep16		   pct_nohs_county pct_college_county med_white med_black pct_male_county"
global full_controls_var7 " pop_dens_county landarea_county age_imb pct_food_stamps_county med_rep16 med_rep12 				   pct_college_county med_white med_black pct_male_county"
global full_controls_var8 " pop_dens_county landarea_county age_imb pct_food_stamps_county med_rep16 med_rep12 pct_nohs_county 					  med_white med_black pct_male_county"
global full_controls_var9 " pop_dens_county landarea_county age_imb pct_food_stamps_county med_rep16 med_rep12 pct_nohs_county pct_college_county 		    med_black pct_male_county"
global full_controls_var10 "pop_dens_county landarea_county age_imb pct_food_stamps_county med_rep16 med_rep12 pct_nohs_county pct_college_county med_white 		  pct_male_county"
global full_controls_var11 "pop_dens_county landarea_county age_imb pct_food_stamps_county med_rep16 med_rep12 pct_nohs_county pct_college_county med_white med_black 				 "
									
global outcomes_pick "sd_sf_dist_fr_home"

* RF *
foreach cl in state_fips {
	* Main outcomes RF

			foreach y of global outcomes_pick {
				foreach controls_set in full_controls_var1 full_controls_var2 full_controls_var3 full_controls_var4 full_controls_var5 full_controls_var6 full_controls_var7 full_controls_var8 full_controls_var9 full_controls_var10 full_controls_var11 {				
					forvalues date=21946/22035 {
					if dow`date'[1]==0{
					global baseline "`y'_sun"
					}					
					if dow`date'[1]==1{
					global baseline "`y'_mon"
					}					
					if dow`date'[1]==2{
					global baseline "`y'_tue"
					}					
					if dow`date'[1]==3{
					global baseline "`y'_wed"
					}					
					if dow`date'[1]==4{
					global baseline "`y'_thu"
					}					
					if dow`date'[1]==5{
					global baseline "`y'_fri"
					}					
					if dow`date'[1]==6{
					global baseline "`y'_sat"
					}
					cap qui reghdfe `y'`date' fxnc [aweight=pop], absorb(state_fips one##c.($baseline ${`controls_set'} msnbc_rtg cnn_rtg)) cluster(`cl')
					macro drop baseline
					}

			}
		*
		}
*
	
}
*
}
*

* RC POLYNOMIALS AND INTERACTIONS	*
global rc_polynomials_interactions=1
if ${rc_polynomials_interactions}==1 {

foreach check_var in pop_dens_county landarea_county pct_food_stamps_county pct_nohs_county med_white {
cap gen `check_var'_2=`check_var'^2	
cap gen `check_var'_3=`check_var'^3	
cap gen `check_var'_fxnc=`check_var'*fxnc
}

global full_controls_var1 "pop_dens_county_2 pop_dens_county_3"
global full_controls_var2 "landarea_county_2 landarea_county_3"
global full_controls_var3 "pct_food_stamps_county_2 pct_food_stamps_county_3"
global full_controls_var4 "pct_nohs_county_2 pct_nohs_county_3"
global full_controls_var5 "med_white_2 med_white_3"
global full_controls_var6 "pop_dens_county_fxnc"
global full_controls_var7 "landarea_county_fxnc"
global full_controls_var8 "pct_food_stamps_county_fxnc"
global full_controls_var9 "pct_nohs_county_fxnc"
global full_controls_var10 "med_white_fxnc"

global outcomes_pick "sd_sf_dist_fr_home sd_sf_median_nohome_time"

* RF *
foreach cl in state_fips {
	* Main outcomes RF

			foreach y of global outcomes_pick {
				foreach controls_set in full_controls_var1 full_controls_var2 full_controls_var3 full_controls_var4 full_controls_var5 full_controls_var6 full_controls_var7 full_controls_var8 full_controls_var9 full_controls_var10 {				
					forvalues date=21946/22035 {
					if dow`date'[1]==0{
					global baseline "`y'_sun"
					}					
					if dow`date'[1]==1{
					global baseline "`y'_mon"
					}					
					if dow`date'[1]==2{
					global baseline "`y'_tue"
					}					
					if dow`date'[1]==3{
					global baseline "`y'_wed"
					}					
					if dow`date'[1]==4{
					global baseline "`y'_thu"
					}					
					if dow`date'[1]==5{
					global baseline "`y'_fri"
					}					
					if dow`date'[1]==6{
					global baseline "`y'_sat"
					}
					cap qui reghdfe `y'`date' fxnc [aweight=pop], absorb(state_fips one##c.($baseline $full_controls1 ${`controls_set'} msnbc_rtg cnn_rtg)) cluster(`cl')
					macro drop baseline
					}
				}
			}
		*
		}
*
}
*

* STATE REGS				
global outcomes_pick "sd_sf_dist_fr_home"
* RF *
foreach cl in state_fips {

			foreach y of global outcomes_pick {
				foreach state in 1 2 4 5 6 8 9 10 12 13 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 44 45 46 47 48 49 50 51 53 54 55 56 {				
					forvalues date=21946/22035 {	
					if dow`date'[1]==0{
					global baseline "`y'_sun"
					}					
					if dow`date'[1]==1{
					global baseline "`y'_mon"
					}					
					if dow`date'[1]==2{
					global baseline "`y'_tue"
					}					
					if dow`date'[1]==3{
					global baseline "`y'_wed"
					}					
					if dow`date'[1]==4{
					global baseline "`y'_thu"
					}					
					if dow`date'[1]==5{
					global baseline "`y'_fri"
					}					
					if dow`date'[1]==6{
					global baseline "`y'_sat"
					}
					cap qui reghdfe `y'`date' fxnc [aweight=pop] if state_fips!=`state', absorb(state_fips one##c.($baseline $full_controls1 msnbc_rtg cnn_rtg)) cluster(`cl')
					macro drop baseline
					}

			}
		*
		}
*
		*
		}
*




















